// packageF/tourAround/tourSearch/tourSearch.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    discoverInfo: [],
    // 热门搜索
    hotInfo: [],
    hotPage: 1,
    hotLastPage: 0,
    hotTap: false,
    keyWord: "",
    searchHistory: [],
    city:'',
    latitude:'',
    longitude:'',
    cityCode:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if ((options.keyWord ?? "") !== "") {
      this.setData({
        keyWord: (options.keyWord).trim()
      })
    }
    if ((options.city ?? "") !== "") {
      this.setData({
        city:options.city,
        latitude:options.latitude,
        longitude:options.longitude,
        cityCode:options.cityCode
      })
    }
    this.setData({
      city:options.city,
      cityCode:options.cityCode,
      latitude:options.latitude,
      longitude:options.longitude
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  searchKey(event) {
    this.setData({
      keyWord: event.detail.value
    })
  },
  confirmTap(event) {
    this.setData({
      keyWord: event.detail.value
    })
    if(this.data.keyWord) {
      wx.navigateTo({
        url: `/packageF/tourAround/tourSearchPage/tourSearchPage?keyword=${this.data.keyWord}&city=${this.data.city}&latitude=${this.data.latitude}&longitude=${this.data.longitude}&cityCode=${this.data.cityCode}`,
      })
    }
  },
  goSearch() {
    if(this.data.keyWord) {
      wx.navigateTo({
        url: `/packageF/tourAround/tourSearchPage/tourSearchPage?keyword=${this.data.keyWord}&city=${this.data.city}&latitude=${this.data.latitude}&longitude=${this.data.longitude}&cityCode=${this.data.cityCode}`,
      })
    }
  },
  tapKeyWord(event) {
    let name = event.currentTarget.dataset.name
    wx.navigateTo({
      url: `/packageF/tourAround/tourSearchPage/tourSearchPage?keyword=${name}&city=${this.data.city}&latitude=${this.data.latitude}&longitude=${this.data.longitude}&cityCode=${this.data.cityCode}`,
    })
  },
  deleHistory() {
    wx.setStorageSync('searchHistory') || [];
    this.setData({
      searchHistory: []
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    try {
      let History = wx.getStorageSync("searchHistory") || [];
        this.setData({
          searchHistory: JSON.parse(History)
        })
    } catch (error) {
      console.log(error);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})